package com.passioneffect.widgets {
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import gs.TweenMax;	

	/**
	 * @author Passion Effect
	 */
	public class RectGrow {

		public var length : Number = 0;
		protected var _color : Number = 0x000000;
		protected var _alpha : Number = 1;
		private var tlength:Number=0;
		public var target : DisplayObjectContainer;
		protected var eff : Sprite;
		public var opened : Boolean;
		private var _time : Number = 0.2;
		protected var _delayClose:Number=0;
		protected var type:String="pixel";//pixel/percent
		public var offsetBottom:Number=0;
		public var toffsetBottom:Number=0;
		
		public function RectGrow(mc : DisplayObjectContainer,length : *,color : Number,alpha : Number = 1,time : Number = 0.2,delayClose:Number=0) {
			if(length is String){
				type="percent";
				tlength=Number(length);
			}else{
				tlength=length;
			}
			
			target = mc;			
			_color = color;
			_alpha = alpha;
			
			_time = time;
			_delayClose=delayClose;
			eff = new Sprite();
			eff.mouseEnabled = false;
		}
		
		public function setEvents():void{
			target.addEventListener(MouseEvent.MOUSE_MOVE, over);
			target.addEventListener(MouseEvent.MOUSE_OUT, out);
		}
		
		private function out(event : MouseEvent) : void {
			this.close();
		}

		private function over(event : MouseEvent) : void {
			this.open();
		}

		public function open(onComplete:Function=null) : void {
			if(opened)return;
			opened = true;
			
			if(target.parent)target.parent.addChild(eff);
			TweenMax.to(this,_time,{overwrite:1,onComplete:onComplete,offsetBottom:toffsetBottom,length:tlength,onUpdate:draw});
		}

		public function close() : void {
			if(!opened)return;
			opened = false;
			TweenMax.to(this,_time,{offsetBottom:0,length:0,onUpdate:draw});
		}

		public function removeRect() : void {
			
		}

		public function draw(evt : Event = null) : void {
			var l:Number=length;
			if(type=="percent"){
				l=target.height*length;
			}
			
			var g : Graphics = eff.graphics;
			g.clear();
			g.lineStyle();
			g.beginFill(_color, _alpha);
			g.drawRect(target.x,target.y,target.width,target.height);
			g.drawRect(target.x-l,target.y-l,target.width+l*2,target.height+l*2+offsetBottom);
			g.endFill();
			
		}

		public function get color() : Number {
			return _color;
		}

		public function set color(color : Number) : void {
			_color = color;
		}

		public function get alpha() : Number {
			return _alpha;
		}

		public function set alpha(alpha : Number) : void {
			_alpha = alpha;
		}
	}
}
